package com.example.graduatetest.model;

import android.text.TextUtils;
import android.util.Log;

import com.example.graduatetest.bean.Card;
import com.example.graduatetest.bean.Classes;
import com.example.graduatetest.bean.Student;
import com.example.graduatetest.callback.CardCallBack;
import com.example.graduatetest.callback.CardListCallBack;
import com.example.graduatetest.callback.ClassList;
import com.example.graduatetest.callback.OneClassCallBack;
import com.example.graduatetest.callback.StringCallBack;
import com.example.graduatetest.callback.StudentList;
import com.example.graduatetest.utils.Constant;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class CardModel {
    private static String www = Constant.www;
    private static String oneClassUrl = "/class/selectOneClass?id=";
    private static String insertOneUrl = "/card/insertOneCard";
    private static String setStatus = "/card/setStatus";
    private static String cardListUrl = "/card/getCardList";
    private static String cardUrl = "/card/getCard";
    private static String punchCardUrl = "/card/punchCard";
    private static String askUrl = "/card/changeAsk";

    //打卡
    public static void punchCard(final String student_number,final String class_number,final String code,final String status,final StringCallBack callBack){
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();//新建一个OKHttp的对象
                RequestBody body = new FormBody.Builder()
                        .add("student_number",student_number)
                        .add("class_number", class_number)
                        .add("code", code)
                        .add("status", status)
                        .build();
                Request request = new Request.Builder()
                        .url(www+punchCardUrl)
                        .post(body)
                        .build();//创建一个Request对象
                try {
                    Response response = client.newCall(request).execute();//发送请求获取返回数据
                    String responseData = response.body().string();//处理返回的数据
                    callBack.onSuccess(responseData);
                } catch (IOException e) {
                    callBack.onFailed();
                    e.printStackTrace();
                }
            }
        }).start();
    }
    //Ask
    public static void askCard(final String student_number,final String class_number,final String code,final StringCallBack callBack){
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();//新建一个OKHttp的对象
                RequestBody body = new FormBody.Builder()
                        .add("student_number",student_number)
                        .add("class_number", class_number)
                        .add("code", code)
                        .build();
                Request request = new Request.Builder()
                        .url(www+askUrl)
                        .post(body)
                        .build();//创建一个Request对象
                try {
                    Response response = client.newCall(request).execute();//发送请求获取返回数据
                    String responseData = response.body().string();//处理返回的数据
                    callBack.onSuccess(responseData);
                } catch (IOException e) {
                    callBack.onFailed();
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //得到card列表
    public static void getCardList(final String teacher_code, final CardListCallBack callBack){
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient = new OkHttpClient();
                Request.Builder reqBuild = new Request.Builder();
                HttpUrl.Builder urlBuilder = HttpUrl.parse(www+cardListUrl)
                        .newBuilder();
                urlBuilder.addQueryParameter("teacher_code", teacher_code);
                reqBuild.url(urlBuilder.build());
                Request request = reqBuild.build();
                Call call = okHttpClient.newCall(request);
                try {
                    Response response = call.execute();
                    String jsonData = response.body().string();
                    parseJsonCardList(jsonData, callBack);
                } catch (IOException e) {
                    callBack.onFailed();
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //获取一个card的信息
    public static void getOneCard(final String class_number, final String code, final CardCallBack callBack){
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient = new OkHttpClient();
                Request.Builder reqBuild = new Request.Builder();
                HttpUrl.Builder urlBuilder = HttpUrl.parse(www+cardUrl)
                        .newBuilder();
                urlBuilder.addQueryParameter("class_number", class_number);
                urlBuilder.addQueryParameter("code", code);
                reqBuild.url(urlBuilder.build());
                Request request = reqBuild.build();
                Call call = okHttpClient.newCall(request);
                try {
                    Response response = call.execute();
                    String jsonData = response.body().string();
                    parseJSONCard(jsonData, callBack);
                } catch (IOException e) {
                    callBack.onFailed();
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //修改card状态
    public static void setStatusFunction(final String id,final StringCallBack callBack){
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();//新建一个OKHttp的对象
                RequestBody body = new FormBody.Builder()
                        .add("id",id)
                        .build();
                Request request = new Request.Builder()
                        .url(www+setStatus)
                        .post(body)
                        .build();//创建一个Request对象
                try {
                    Response response = client.newCall(request).execute();//发送请求获取返回数据
                    String responseData = response.body().string();//处理返回的数据
                    callBack.onSuccess(responseData);
                } catch (IOException e) {
                    callBack.onFailed();
                    e.printStackTrace();
                }
            }
        }).start();
    }


    //得到课程信息
    public static void getOneClass(final String id, final OneClassCallBack callBack){
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(www+oneClassUrl+id)
                        .build();
                Call call = okHttpClient.newCall(request);
                try {
                    Response response = call.execute();
                    String jsonData = response.body().string();
                    parseJSONClasses(jsonData,callBack);
                } catch (IOException e) {
                    callBack.onFailed();
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //添加card记录
    public static void addOneCard(final Card card, final StringCallBack callBack){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    OkHttpClient client = new OkHttpClient();//新建一个OKHttp的对象
                    RequestBody body = new FormBody.Builder()
                            .add("time",card.getTime())
                            .add("code",card.getCode())
                            .add("all_number",String.valueOf(card.getAll_number()))
                            .add("teacher_code", card.getTeacher_code())
                            .add("class_number", card.getClass_number())
                            .add("stay_s", String.valueOf(card.getStay_s()))
                            .add("set_meter", String.valueOf(card.getSet_meter()))
                            .add("address", card.getAddress())
                            .add("lat", String.valueOf(card.getLat()))
                            .add("lon", String.valueOf(card.getLon()))
                            .build();
                    Request request = new Request.Builder()
                            .url(www+insertOneUrl)
                            .post(body)
                            .build();//创建一个Request对象

                    Response response = client.newCall(request).execute();//发送请求获取返回数据
                    String responseData = response.body().string();//处理返回的数据
                    Log.e("haha", responseData);
                    callBack.onSuccess(responseData);

                }catch (Exception e){
                    callBack.onFailed();
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //解析Classes json对象
    private static void parseJSONClasses(String jsonData,OneClassCallBack callBack){
        try{
            JSONObject jsonObject = new JSONObject(jsonData);
            Gson gson = new Gson();//实例化gson
            //解析classes对象
            Classes classes = gson.fromJson(jsonObject.toString(),Classes.class);
            callBack.onSuccess(classes);
        }catch (Exception e){
            callBack.onFailed();
            e.printStackTrace();
        }
    }

    //解析Card json对象
    private static void parseJSONCard(String jsonData,CardCallBack callBack){
        try{
            if(!TextUtils.isEmpty(jsonData)){
                JSONObject jsonObject = new JSONObject(jsonData);
                Gson gson = new Gson();//实例化gson
                //解析card对象
                Card card = gson.fromJson(jsonObject.toString(),Card.class);
                callBack.onSuccess(card);
            }
        }catch (Exception e){
            callBack.onFailed();
            e.printStackTrace();
        }
    }

    //解析List<Card> json对象
    private static void parseJsonCardList(String jsonData,CardListCallBack callBack){
        try{
            JSONArray jsonArray = new JSONArray(jsonData);
            Gson gson = new Gson();//实例化gson
            //解析Classes对象
            Log.e("haha", jsonArray.toString());
            List<Card> cardList = gson.fromJson(jsonArray.toString(), new TypeToken<List<Card>>(){}.getType());
            callBack.onSuccess(cardList);
        }catch (Exception e){
            callBack.onFailed();
            e.printStackTrace();
        }
    }
}
